// 运行时配置

// 全局初始化数据配置，用于 Layout 用户信息和权限初始化
// 更多信息见文档：https://next.umijs.org/docs/api/runtime-config#getinitialstate


import React from 'react';
import { Provider } from 'react-redux';
import store from './redux/store';
import { history } from '@umijs/max';
import { queryUserInfo } from './services/demo/UserController/login';

const loginPath = '/user/login'

export async function getInitialState(): Promise<{ 
  name: string;
  currentUser?: API.CurrentUser;
  fetchUserInfo?: () => Promise<{} | undefined>;
 }> {

  const fetchUserInfo = async () => {
    try {
      const msg = await queryUserInfo();
      return msg.data;
    } catch (error) {
      history.push(loginPath);
    }
    return undefined
  }
    // 如果是登录页面，不执行
    if (history.location.pathname !== loginPath) {
      const currentUser = await fetchUserInfo();
      return {
        fetchUserInfo,
        currentUser,
        name: '远野见子',    
      };
    }
  
  return { 
    fetchUserInfo,
    name: '远野见子',
  };
}

export const layout = () => {
  return {
    logo: 'https://img.alicdn.com/tfs/TB1YHEpwUT1gK0jSZFhXXaAtVXa-28-27.svg',
    menu: {
      locale: false,
    },
  };
};


const ReduxProvider = (props: any) => <Provider store={store} {...props} />;
export function rootContainer(container: any, opts: any) {
  return React.createElement(ReduxProvider, opts, container);
}